package structure.stack;

/**
* @description: 使用数组模拟栈结构
* @author: wangjun
* @create: 2022-04-21
*/
public class StackByArray {
    /**栈的最大容量*/
    private Integer maxSize;
    /**存放数据栈*/
    private int[] stack;
    /**初始指针*/
    private int top = -1;

    public StackByArray(int maxSize){
        this.maxSize = maxSize;
        this.stack = new int[maxSize];
    }

    public StackByArray() {

    }

    public boolean isFull(){
        return top == maxSize - 1;
    }

    public boolean isEmpty(){
        return top == -1;
    }

    public void push(int value){
        if(isFull()){
            System.out.println("栈已满");
            return;
        }
        top++;
        stack[top] = value;
    }

    public int pop(){
        if(isEmpty()){
            System.out.println("栈为空");
            return -1;
        }
        int value = stack[top];
        top--;
        return value;
    }

    public void show(){
        if(isEmpty()){
            System.out.println("栈为空");
            return;
        }
        for (int i = top; i >= 0; i--) {
            System.out.printf("stack[%d]=%d\n",i,stack[i]);
        }
    }
}